<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="发布人" prop="userName">
        <el-input
            v-model="queryParams.userName"
            placeholder="请输入发布人"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="userPhone">
        <el-input
            v-model="queryParams.userPhone"
            placeholder="请输入手机号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布时间" prop="validTimeRange">
        <el-date-picker
            clearable
            v-model="validTimeRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
            @change="handleValidTimeChange"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="trendsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="发布人" align="center" prop="userName" />
      <el-table-column label="手机号" align="center" prop="userPhone" />
      <el-table-column label="点赞数" align="center" prop="likes" />
      <el-table-column label="收藏数" align="center" prop="collects" />
      <el-table-column label="评论数" align="center" prop="contents" />
      <el-table-column label="发布时间" width="180" align="center" prop="createTime"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleView(scope.row, scope.index)" v-hasPermi="['system:tytrends:query']">查看</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:tytrends:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改动态对话框 -->
    <el-dialog :title="title" v-model="viewOpen" width="800px" append-to-body>
      <el-form ref="trendsRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
        <el-form-item label="发布人" prop="userName">
          <el-input v-model="form.userName" :disabled="true"/>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="手机号" prop="userPhone">
          <el-input v-model="form.userPhone" :disabled="true"/>
        </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" :disabled="true" />
        </el-form-item>
        <el-form-item label="详情图" prop="pic">
          <image-preview :src-list="form.pic" :width="150" :height="150"/>
        </el-form-item>
        <el-form-item label="内容" prop="body">
          <editor v-model="form.body" :min-height="192" :readOnly="true"/>
        </el-form-item>
        <el-form-item label="地点" prop="location">
          <el-input v-model="form.location" :disabled="true" />
        </el-form-item>
        <el-form-item label="可见性" prop="isPublicLabel">
          <el-input v-model="form.isPublicLabel" :disabled="true" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="viewOpen = false">关闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Trends">
import { listTrends, getTrends, delTrends, addTrends, updateTrends } from "@/api/system/tyTrends";
import {getTshop} from "@/api/system/tshop";
import {ref} from "vue";
import {ElMessage} from "element-plus";

const { proxy } = getCurrentInstance();

const trendsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const viewOpen = ref(false);
const validTimeRange = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    uid: null,
    mid: null,
    isPublic: null,
    pic: null,
    title: null,
    content: null,
    body: null,
    like: null,
    likes: null,
    forward: null,
    collect: null,
    collects: null,
    coordinate: null,
    location: null,
    status: null,
    deleted: null
  },
  rules: {
    updateTime: [
      { required: true, message: "更新时间不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询动态列表 */
function getList() {
  loading.value = true;
  listTrends(queryParams.value).then(response => {
    for (var item of response.rows) {
      item.pic = item.pic.split(',');
    }
    trendsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    uid: null,
    mid: null,
    isPublic: null,
    pic: null,
    title: null,
    content: null,
    body: null,
    like: null,
    likes: null,
    forward: null,
    collect: null,
    collects: null,
    coordinate: null,
    location: null,
    createTime: null,
    updateTime: null,
    status: null,
    deleted: null
  };
  proxy.resetForm("trendsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  validTimeRange.value = [];
  queryParams.value.startTime = null;
  queryParams.value.endTime = null;
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加动态";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getTrends(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改动态";
  });
}

function handleView(row) {
  const _id = row.id || ids.value
  form.value = row;
  viewOpen.value = true;
  // getTrends(_id).then(response => {
  //   response.data.shopDetailPic = response.data.shopDetailPic.split(',')
  //   form.value = response.data;
  //   viewOpen.value = true;
  //
  // });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["trendsRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateTrends(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addTrends(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除该数据项？').then(function() {
    return delTrends(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/trends/export', {
    ...queryParams.value
  }, `trends_${new Date().getTime()}.xlsx`)
}

function handleValidTimeChange(value) {
  // if (value[0] < form.value.saleStartTime) {
  //   ElMessage.error('活动开始时间不能小于报名开始时间哦！')
  // }
  // if (value[1] < form.value.saleEndTime) {
  //   ElMessage.error('活动结束时间不能小于报名结束时间哦！')
  // }
  if (value && value.length === 2) {
    queryParams.value.startTime = value[0];
    queryParams.value.endTime = value[1];
  }else{
    queryParams.value.startTime = null;
    queryParams.value.endTime = null;
  }
}

getList();
</script>
